package com.ruoyi.business.order.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.ruoyi.business.base.JoBaseServiceImpl;
import com.ruoyi.business.order.domain.JbOrder;
import com.ruoyi.business.order.mapper.JbOrderMapper;
import com.ruoyi.business.order.service.JbOrderService;
import com.ruoyi.common.config.JoConfig;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 订单Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Service
public class JbOrderServiceImpl
        extends JoBaseServiceImpl<JbOrderMapper, JbOrder>
        implements JbOrderService
{

    @Autowired
    private JoConfig joConfig;

    @Override
    public JSONObject findBusinessByProcessId(String processId)
    {
        String sql = """
                SELECT
                	jo.process_number processId,
                	jo.order_number businessCode,
                	jo.id\s
                FROM
                	jb_order jo\s
                WHERE
                	jo.process_number = '#{processId}' UNION
                SELECT
                	jr.process_number processId,
                	jr.refund_number businessCode,
                	jr.id\s
                FROM
                	jb_refund jr\s
                WHERE
                	jr.process_number = '#{processId}' UNION
                SELECT
                	jpr.process_number processId,
                	jpr.payment_number businessCode,
                	jpr.id\s
                FROM
                	jb_payment_received jpr\s
                WHERE
                	jpr.process_number = '#{processId}'
                """;
        String sqlStr = joSqlUtil.sqlJson(sql, new JSONObject().fluentPut("processId", processId));
        List<JSONObject> list = baseMapper.findJson(sqlStr);
        if (list.isEmpty())
        {
            return null;
        }
        return list.get(0);
    }

    @Override
    public JSONObject findProcess(String processInstanceId)
    {
        String sql = """
                SELECT
                	fpiaur.process_instance_id processInstanceId,
                	fpiaur.flow_id flowId\s
                FROM
                	{}.flyflow_process_instance_assign_user_record fpiaur\s
                WHERE
                	fpiaur.process_instance_id = '#{processInstanceId}'
                """;
        sql = StringUtils.format(sql, joConfig.getFlyflow());
        String sqlStr = joSqlUtil.sqlJson(sql, new JSONObject().fluentPut("processInstanceId", processInstanceId));
        List<JSONObject> list = baseMapper.findJson(sqlStr);
        if (list.isEmpty())
        {
            return null;
        }
        return list.get(0);
    }
}
